# -*- coding: UTF-8 -*-
'''
Created on 2011-8-21

@author: zxz
'''
from flask import Flask, session, request

from datasource import db_session

from app import app
from locationModel import LocationPlace
from common.jsonUtil import success, error
from common.locationUtil import range

MAX_SIZE = 10
ADDRESS_RANGE = 200

@app.route('/location/', methods=['GET'])
def location_test():
    ''' 测试 '''
    return success({'location' : 'location test'})

@app.route('/location/<float:latitude>/<float:longitude>/', methods=['GET'])
def location_list(latitude, longitude):
    ''' 获得用户定义的地理位置信息列表 '''
    latLngRange = range(latitude, longitude, ADDRESS_RANGE)
    if latLngRange is None:
        return error(u'地理位置不合法')

    locations = db_session.query(LocationPlace).\
    filter(LocationPlace.latitude < latLngRange['maxLat']).filter(LocationPlace.latitude > latLngRange['minLat']).\
    filter(LocationPlace.longitude < latLngRange['maxLng']).filter(LocationPlace.longitude > latLngRange['minLng']).\
    order_by(LocationPlace.use_cnt.desc()).offset(0).limit(MAX_SIZE)
    locationEntrys = [l.entry() for l in locations]
    return success({'locations':locationEntrys})

